package com.cby.jetpack.sample.worker

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer
import androidx.work.Data
import androidx.work.OneTimeWorkRequest
import androidx.work.WorkManager
import com.cby.jetpack.sample.R
import java.util.concurrent.TimeUnit
import androidx.work.WorkInfo
import com.cby.util.lib.util.CbyLogUtils
import androidx.work.PeriodicWorkRequest





/**
 * @description: WorkManagerActivity
 *
 * @author ChenBaoyang
 * @date 2021/10/11 10:19
 */
class WorkManagerActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_work_manager)
    }

    fun onWorkTest1(view: android.view.View) {
        val oneTimeWorkRequest = OneTimeWorkRequest.Builder(SimpleWorker::class.java)
            //延迟2秒
            .setInitialDelay(2, TimeUnit.SECONDS)
            .build()
        WorkManager.getInstance(this@WorkManagerActivity)
            .enqueue(oneTimeWorkRequest)
    }

    fun onWorkTest2(view: android.view.View) {
        val data = Data.Builder()
            .putString("data", "Activity的数据").build()
        val oneTimeWorkRequest = OneTimeWorkRequest.Builder(TransferDataWorker::class.java).setInputData(data).build()
        WorkManager.getInstance(this@WorkManagerActivity).enqueue(oneTimeWorkRequest)
        //数据接收
        WorkManager.getInstance(this)
            .getWorkInfoByIdLiveData(oneTimeWorkRequest.id)
            .observe(this, { workInfo ->
                if (workInfo != null) {
                    if (workInfo.state.isFinished) {
                        val outputData = workInfo.outputData
                        CbyLogUtils.flog("传递数据任务", outputData.getString("data"))
                    }
                }
            })
    }

    fun onWorkTest3(view: android.view.View) {
        val workerRequests: ArrayList<OneTimeWorkRequest> = ArrayList()
        //任务1
        val oneTimeWorkRequest1 = OneTimeWorkRequest.Builder(MoreWork::class.java)
            .setInputData(Data.Builder().putString("data", "任务1").build())
            .build()
        //任务2
        val oneTimeWorkRequest2 = OneTimeWorkRequest.Builder(MoreWork::class.java)
            .setInputData(Data.Builder().putString("data", "任务2").build())
            .build()
        workerRequests.add(oneTimeWorkRequest1)
        workerRequests.add(oneTimeWorkRequest2)
        //任务3
        val oneTimeWorkRequest3 = OneTimeWorkRequest.Builder(MoreWork::class.java)
            .setInputData(Data.Builder().putString("data", "任务3").build())
            .build()
        WorkManager.getInstance(this)
            .beginWith(workerRequests) //先执行的任务
            .then(oneTimeWorkRequest3) //后执行的任务
            .enqueue()
    }

    fun onWorkTest4(view: android.view.View) {
        //需要注意的是，周期性任务的间隔时间不能少于15分钟
        val periodicWorkRequest =
            PeriodicWorkRequest.Builder(PeriodicWork::class.java, 15, TimeUnit.MINUTES)
                .build()
        WorkManager.getInstance(this)
            .enqueue(periodicWorkRequest)
    }

    fun onWorkTest5(view: android.view.View) {
        val oneTimeWorkRequest = OneTimeWorkRequest.Builder(SimpleWorker::class.java)
            //延迟2秒
            .setInitialDelay(2, TimeUnit.SECONDS)
            .build()
        //根据id取消
        WorkManager.getInstance(this).cancelWorkById(oneTimeWorkRequest.getId());
        //取消所有
        WorkManager.getInstance(this).cancelAllWork();
        //根据Tag取消
        WorkManager.getInstance(this).cancelAllWorkByTag("Tag");
    }

}